package com.wqtz.chechai.base.lvbaseadapter;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/13 0:48
 */

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wqtz.chechai.R;
import com.wqtz.chechai.widget.GlideRoundTransform;


public class ViewHolder {

	View myConvertView;
	//创建SparseArray 对象用于保存view对象
	SparseArray<View> list=new SparseArray<>();
	public ViewHolder(int resId, Context context) {
		myConvertView = LayoutInflater.from(context).inflate(resId, null);
		myConvertView.setTag(this);

	}

	//返回myConvertView
	public  View getViewHolder(){
		return myConvertView;
	}

	//返回Viewholder对象
	public static ViewHolder getInstance(int resId, Context context, View convertView) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder(resId, context);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		return holder;
	}

	//返回当前id的view，如果不能在list中找到，则到布局找
	public View getView(int id)
	{
		View view = list.get(id);
		if (view==null)
		{
			view= myConvertView.findViewById(id);
			list.put(id,view);
		}
		return view;
	}
	//给v指定id的view添加字符串
	public void setText(int id,String text)
	{
		if (text!=null) {
			((TextView) getView(id)).setText(text);
		}

	}

	public void setImage(int id,String url)
	{
		if (url!=null)
		{

			ImageView imageView = (ImageView) getView(id);

			Glide.with(imageView.getContext())
					.load(url)
					.thumbnail(0.1f)//加载略缩图
					.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存所有图片
					.placeholder(R.mipmap.ic_launcher)//加载前的默认图片
					.error(R.mipmap.ic_launcher)//下载错误时的替换
					.into(imageView);
		}
	}

	public void setCirCleImag(int id, int resId){
		if (resId != 0) {
			ImageView imageView = (ImageView) getView(id);
			RequestManager glideRequest= Glide.with(imageView.getContext());
			glideRequest.load(resId).
					error(R.mipmap.addimage)//下载错误时的替换
					.dontAnimate() //不加载动画效果
					.fitCenter()  //默认全屏居中
					.transform(new GlideRoundTransform(imageView.getContext(), 5)).into(imageView);
		}
	}


}
